<?php

namespace App\Modules\Store\Controllers;

use App\Exceptions\AllValidatorException;
use App\Lib\Facades\FLAuth;
use App\Lib\Utilities;
use App\Modules\Employee\Services\EmployeeService;
use App\Modules\Employee\Transformers\EmployeeTransformer;
use App\Modules\Employee\Transformers\GetMostPopularStoreTransformer;
use App\Modules\Store\Services\StoreService;
use App\Modules\Store\Validators\StoreControllerValidator;
use Flower\Core\Http\BaseController;
use Flower\Core\Repository\Exceptions\ValidatorException;
use Illuminate\Http\Request;
use Illuminate\Http\Response;
use Illuminate\Routing\Controller;
use App\Modules\Employee\Validators\PortfolioValidator;
class StoreController extends BaseController
{
    private $storeService;
    private $employeeService;

    public function getRules()
    {
        return StoreControllerValidator::validator();
    }

    public function __construct(StoreService $storeService,
                                EmployeeService $employeeService)
    {
        $this->storeService = $storeService;
        $this->employeeService = $employeeService;
    }

    //创建店铺
    public function createStore(Request $request)
    {
        $name = $request->input('name');
        $phone = $request->input('phone','');
        $wechat = $request->input('wechat');
        $profile = $request->input('profile');
        $longitude = $request->input('longitude');
        $latitude = $request->input('latitude');
        $invite_code = $request->input('invite_code');
        $adcode = $request->input('adcode');
        $uid = $request->input('uid');
        $address = $request->input('address');
        $store = $this->storeService
            ->createStore(
                $name,
                $phone,
                $wechat,
                $profile,
                $longitude,
                $latitude,
                $invite_code,
                $adcode,
                $uid,
                $address
            );

        $store_id = $store->id;

        $user_id = FLAuth::id();

        $user = FLAuth::user();

        $role_info = $this->employeeService->getRoleInfo($user_id, $store_id, config('constant.role.manager'));

        return $this->display((new EmployeeTransformer($user, $role_info))->transform(), '创建成功');


    }

    //修改店铺信息
    public function updateStore(Request $request)
    {
        $this->storeService->updateStore($request->all());
        return $this->success('修改成功');
    }

    //保存店铺介绍
    public function createStoreProfile(Request $request)
    {
        $store_id = $request->input('store_id');
        $image = $request->input('image');
        $video = $request->input('video');

        $this->storeService->createStoreProfile($store_id, $image, $video);

        return $this->success('保存成功');
    }

    /**
     * 返回店铺介绍 (一个视频三个图片)
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     */
    public function getStoreProfile(Request $request)
    {
        $store_id = $request->input('store_id');
        $data = $this->storeService->getStoreProfile($store_id);
        return $this->display($data, '查询成功');
    }


	/**
	 * App端获取店铺基本信息
	 * @param Request $request
	 * @return mixed
	 * @throws \Flower\Core\Repository\Exceptions\RepositoryException
	 */
    public function getStoreInfoApp(Request $request)
    {
        $store_id = $request->input('store_id');

        $data = $this->storeService->getStoreInfoApp($store_id);

        return $this->display($data, '查询成功');
    }

    /**
     * 获取店铺收款码
     * @param Request $request
     */
    public function getStorePaymentCode(Request $request)
    {
        $store_id = $request->input('store_id');

        $data = $this->storeService->getStorePaymentCode($store_id);

        return $this->display($data, '查询成功');
    }


    /**
     * 关闭店铺
     * @param Request $request
     * @return mixed
     */
    public function closeOpenStore(Request $request)
    {
        $store_id = $request->input('store_id');
        $operation = $request->input('operation');
        $this->storeService->closeOpenStore($store_id,$operation);
        return $this->success('操作成功');
    }

    /**
     * 获取店铺列表
     * @param Request $request
     * @return mixed
     */
    public function getStoreList(Request $request)
    {
        $user_lng = $request->input('longitude',35);
        $user_lat = $request->input('latitude',120);
        $adcode = $request->input('adcode',120);
        $orderBy = $request->input('orderBy',1);
        $pageSize = $request->input('pageSize',10);
        $keywords = $request->input('keywords');
        $res = $this->storeService->getStoreList($user_lng,$user_lat,$adcode,$orderBy,$keywords,$pageSize);
        return $this->success($res);
    }

    /**
     * 获取店铺首页信息 wx
     * @param Request $request
     * @return mixed
     * @throws AllValidatorException
     */
    public function getStoreInfo(Request $request)
    {
        $store_id = $request->input('store_id');
        $info = $this->storeService->getStoreInfo($store_id);
        $storeInfo = $info->toArray();
        if(isset($storeInfo['store_resource'])){
	        $storeInfo['store_resource'] = array_map(function ($v){
	        	if(empty($v['video'])) unset($v['video']);
	        	return $v;
	        },$storeInfo['store_resource']);
        };
        return $this->display($storeInfo, '查询成功');
    }

    /**
     * 关注店铺
     * @param Request $request
     * @return mixed
     * @throws AllValidatorException
     */
    public function likeStore(Request $request)
    {
        $member_id = FLAuth::id();
        $store_id= $request->input('store_id');
        $operation = $request->input('operation');
        $this->storeService->likeStore($member_id,$store_id,$operation);
        return $this->success('操作成功');
    }

    /**
     * 用户获取本人关注的店铺列表
     * @param Request $request
     * @return mixed
     */
    public function getMyLikeStoreList(Request $request)
    {
        $member_id = FLAuth::id();
        $pageSize = $request->input('pageSize',10);
        $list = $this->storeService->getMyLikeStoreList($member_id,$pageSize);
        return $this->display($list, '查询成功');
    }

    /**
     * 获取店铺支付方式提成
     * @param Request $request
     * @return mixed
     */
    public function getStorePaymentRoyalty(Request $request)
    {
        $store_id= $request->input('store_id');

        $data = $this->storeService->getStorePaymentRoyalty($store_id);

        return $this->display($data, '查询成功');
    }

    /**
     * 修改店铺支付方式提成
     * @param Request $request
     * @return mixed
     */
    public function updateStorePaymentRoyalty(Request $request)
    {
        $store_id = $request->input('store_id');
        $wechat = $request->input('wechat');
        $alipay = $request->input('alipay');
        $cash = $request->input('cash');
        $bank_card = $request->input('bank_card');
        $group_buy = $request->input('group_buy');
        $member_card = $request->input('member_card');
        $this->storeService->updateStorePaymentRoyalty(
            $store_id,$wechat,$alipay,$cash,$bank_card,$group_buy,$member_card
        );

        return $this->success('修改成功');
    }

    /**
     * 根据店铺id获取店铺名
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     */
    public function getStoreNameById(Request $request)
    {
        $store_id = $request->input('store_id');
        $data = $this->storeService->getStoreNameById($store_id);

        return $this->display($data, '查询成功');
    }

    /**
     * 上传店铺相册
     * @param Request $request
     * @return mixed
     * @throws
     */
    public function createStorePhoto(Request $request)
    {
        $store_id = $request->input('store_id');
        $image    = $request->input('image');      // 图片地址
        $id = $this->storeService->createStorePhoto($store_id, $image);
        return $this->display($id);
    }

    /**
     * 删除店铺相册
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     */
    public function deleteStorePhoto(Request $request)
    {
        $store_photo_id = $request->input('store_photo_id');
        $this->storeService->deleteStorePhoto($store_photo_id);


        return $this->success('操作成功');

    }

    /**
     * 获取店铺相册
     * @param Request $request
     * @return mixed
     * @throws \App\Exceptions\AllValidatorException
     */
    public function getStorePhoto(Request $request)
    {
        $store_id = $request->input('store_id');
        $pageSize = $request->input('pageSize', 10);

        $data = $this->storeService->getStorePhoto($store_id, $pageSize);
        return $this->display($data, '查询成功');
    }

    /**
     * 设置用户角色
     * @param Request $request
     * @return mixed
     * @throws AllValidatorException
     * @throws \App\Exceptions\NotFoundException
     */
    public function setUserStoreRole(Request $request)
    {
        $res = $this->storeService->setUserStoreRole($request->user_id, $request->store_id, $request->role);
        if (empty($res)) return $this->error("用户角色设置失败");
        return $this->success("用户角色设置成功");
    }

	/**
	 * @param Request $request
	 * @return mixed
	 * @throws AllValidatorException
	 * @throws \App\Exceptions\DbException
	 */
    public function cancellationStore(Request $request)
    {
        $store_id = $request->input('store_id');
        $this->storeService->cancellationStore($store_id);
        return $this->success("操作成功");
    }

    /**
     * 以OSS地址方式上传店铺LOGO
     * @param Request $request
     * @return mixed
     * @throws
     */
    public function updateStoreLogo(Request $request){
        $store_id = $request->input('store_id');
        $logo     = $request->input('logo');
        $this->storeService->updateStoreLogo($store_id,$logo);
        return $this->success("操作成功");
    }


//	public function getStoreLevels(Request $request){
//    	$levels = $this->storeService->getStoreLevel(4500);
//    	dd($levels);
//	}
}
